package student;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


public class StuInfoFrame {
    public static void main(String[] args) {
        JFrame frame = new JFrame("学生信息管理");
        frame.setSize(800,600);
        frame.setLayout(new BorderLayout());
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton importt = new JButton("导入");

        JButton sort = new JButton("排序");

        JButton exit = new JButton("退出");

        JButton search = new JButton("查找");
        JTextArea textArea = new JTextArea();
        JLabel labelNum = new JLabel("学号");
        JRadioButton up=new JRadioButton("升序",true);    //创建JRadioButton对象
        JRadioButton drop=new JRadioButton("降序");
        ButtonGroup group=new ButtonGroup();
        group.add(up);
        group.add(drop);
        JLabel label = new JLabel("学号   姓名  成绩1   成绩2   成绩3");
        JLabel label1 = new JLabel("学号：");
        JTextField textField = new JTextField(20);



        JPanel top = new JPanel();
        JPanel mid = new JPanel();
        JPanel buttom = new JPanel();
        top.add(importt);
        top.add(up);
        top.add(drop);
        top.add(sort);
        top.add(exit);
        mid.add(label);

        mid.add(textArea);
        buttom.add(label1);
        buttom.add(textField);
        buttom.add(search);
        frame.add(top, BorderLayout.NORTH);
        frame.add(mid, BorderLayout.CENTER);
        frame.add(buttom, BorderLayout.SOUTH);
        StudentList list = new StudentList();

        importt.addActionListener((ac) -> {

            textArea.setText("");
            textArea.append(list.toString());

        });
        exit.addActionListener((ac) -> System.exit(0));

        search.addActionListener((ac) -> {

            String text = textField.getText();
            textArea.setText("");
            textArea.append(list.searchForId(text));
        });

        sort.addActionListener((ac) -> {
            List<Student> thisList = list.getList();
            if (up.isSelected()) {

                Collections.sort(thisList,(stu1,stu2) -> {
                    return stu1.getId() - stu2.getId();
                });
            }else {
                Collections.sort(thisList,(stu1,stu2) -> {
                    return stu2.getId() - stu1.getId();
                });
            }
            textArea.setText("");
            String s = list.listToString(thisList);
            textArea.append(s);
        });

    }



}
